<?php

class RegisterController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $this->_helper->layout->disableLayout();
    }

    public function saveAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $username = $this->_getParam('username');
        $password = $this->_getParam('password');
        $repassword = $this->_getParam('repassword');
        $email = $this->_getParam('email');
        $name = $this->_getParam('name');
        $surname = $this->_getParam('surname');
        $day = $this->_getParam('day');
        $month = $this->_getParam('month');
        $year = $this->_getParam('year');
        $career = $this->_getParam('career');

        try
        {
            $conn = Doctrine_Manager::connection();
            $conn->beginTransaction();

            $profile = new Profile();
            $profile->career = $career;
            $profile->name = $name;
            $profile->surname = $surname;
            $profile->save();

            $user = new User();
            $user->email = $email;
            $user->username = $username;
            $user->password = md5($password);
            $user->rol = 'User';
            $user->profile = $profile;
            $user->save();
            
            $object = new Object();
            $object->object_type = Object::WALL;
            $object->user = $user;
            $object->save();
            
            $wall = new Wall();
            $wall->object = $object;
            $wall->save();
            
            $friendship = new Friendship();
            $friendship->id_friend = $user->id;
            $friendship->id_user = $user->id;
            $friendship->status = Friendship::ARE_FRIEND;
            $friendship->save();
            
            $conn->commit();
            
            $this->_redirect('index');
            
        } catch (Exception $e)
        {
            $conn->rollback();

            echo $e->getMessage();
        }
    }

}